package com.zhida.smartcontrol.pagehome.scan;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.Result;
import com.google.zxing.ResultPoint;
import com.google.zxing.client.android.BeepManager;
import com.jay.tools.ui.TopBarView;
import com.jay.tools.utils.EventBusUtils;
import com.jay.tools.utils.LogUtil;
import com.journeyapps.barcodescanner.BarcodeCallback;
import com.journeyapps.barcodescanner.BarcodeResult;
import com.journeyapps.barcodescanner.DecoratedBarcodeView;
import com.journeyapps.barcodescanner.DefaultDecoderFactory;
import com.zhida.smartcontrol.R;
import com.zhida.smartcontrol.mvp.MVPBaseFragment;
import com.zhida.smartcontrol.pagehome.deviceinfo.DeviceInfoFragment;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * author: tanjianpei
 * date: 2019/10/21 021 12:49
 * description: 扫一扫
 */
public class ScanFragment extends MVPBaseFragment<ScanContract.View, ScanPresenter> implements ScanContract.View {
    @BindView(R.id.top)
    TopBarView mTopBar;
    @BindView(R.id.ll_hand)
    LinearLayout mHandView;
    //    @BindView(R.id.scan_view)
//    ZXingScannerView mScanView;
    @BindView(R.id.barcode_scanner)
    DecoratedBarcodeView barcodeView;
//    private BeepManager beepManager;

    private boolean mOnlyScan = true;

    public static ScanFragment newInstance(boolean onlyScan) {
        Bundle args = new Bundle();
        args.putBoolean("only", onlyScan);
        ScanFragment fragment = new ScanFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected Object getLayout() {
        return R.layout.fragment_scan;
    }

    private BarcodeCallback callback = new BarcodeCallback() {
        @Override
        public void barcodeResult(BarcodeResult result) {

            String text = result.getText();
            if (TextUtils.isEmpty(text)) {
                toast("未扫描到二维码");
                return;
            }
            toast(text);
            if (mOnlyScan) {
                if (text.contains(",")) {
                    String[] split = text.split(",");
                    EventBusUtils.post(new ScanResultEvent(split[0], split[1]));
                    pop();
                } else if (text.contains(";")) {
                    String[] split = text.split(";");
                    EventBusUtils.post(new ScanResultEvent(split[0], split[1]));
                    pop();
                } else {
                    toast("错误的二维码");
                }
            } else {
                if (text.contains(",")) {
                    startWithPop(DeviceInfoFragment.newInstance(result.getText()));
                } else if (text.contains(";")) {
                    startWithPop(DeviceInfoFragment.newInstance(result.getText()));
                } else if (text.contains("{") && text.contains("}")) {
                    startWithPop(DeviceInfoFragment.newInstance(result.getText()));
                } else {
                    toast("错误的二维码");
                }
            }
        }
    };

    @Override
    protected void initLazy(Bundle savedInstanceState) {
        inflateTopBar(mTopBar);

        Bundle bundle = getArguments();
        if (null != bundle) {
            mOnlyScan = bundle.getBoolean("only");
            mHandView.setVisibility(mOnlyScan ? View.GONE : View.VISIBLE);
        }

//        List<BarcodeFormat> formats = new ArrayList<>();
//        formats.add(BarcodeFormat.QR_CODE); // 二维码

//        mScanView.setFormats(formats);
//
//        // this paramter will make your HUAWEI phone works great!
//        mScanView.setAspectTolerance(0.2f);

//        Collection<BarcodeFormat> formats = Arrays.asList(BarcodeFormat.QR_CODE, BarcodeFormat.CODE_39);
//        barcodeView.getBarcodeView().setDecoderFactory(new DefaultDecoderFactory(formats));
        barcodeView.decodeContinuous(callback);

//        beepManager = new BeepManager(getActivity());
//        barcodeView.setTorchOn();
    }

    @Override
    public void onResume() {
        super.onResume();
//        mScanView.setResultHandler(this);
//        mScanView.startCamera();
//        mScanView.setAutoFocus(true);
        barcodeView.resume();
    }

    @Override
    public void onPause() {
        super.onPause();
//        mScanView.stopCamera();
        barcodeView.pause();
    }

    private boolean mFlash = false;

    @OnClick({R.id.barcode_scanner, R.id.ll_hand, R.id.iv_shoudaintong})
    public void click(View v) {
        switch (v.getId()) {
            case R.id.barcode_scanner:
//                mScanView.resumeCameraPreview(this);
                barcodeView.resume();
                break;
            case R.id.ll_hand:
                startWithPop(DeviceInfoFragment.newInstance(""));
                break;
            case R.id.iv_shoudaintong:
                mFlash = !mFlash;
                if (mFlash) {
                    barcodeView.setTorchOn();
                } else {
                    barcodeView.setTorchOff();
                }
                break;
        }
    }

//    @Override
//    public void handleResult(Result rawResult) {
//        String text = rawResult.getText();
//        LogUtil.e(TAG, "类型：" + rawResult.getBarcodeFormat().toString() + "，结果：" + text);
//        if (TextUtils.isEmpty(text)) {
//            toast("未扫描到二维码");
//            return;
//        }
//        if (mOnlyScan) {
//            if (text.contains(",")) {
//                String[] split = text.split(",");
//                EventBusUtils.post(new ScanResultEvent(split[0], split[1]));
//                pop();
//            } else if (text.contains(";")) {
//                String[] split = text.split(";");
//                EventBusUtils.post(new ScanResultEvent(split[0], split[1]));
//                pop();
//            } else {
//                toast("错误的二维码");
//            }
//        } else {
//            startWithPop(DeviceInfoFragment.newInstance(rawResult.getText()));
//        }
//    }

}
